package database;

import java.sql.*;

import oracle.jdbc.pool.OracleDataSource;



public class MainOperations 
{
	private Connection conn;
	private Statement stm;
	
	public MainOperations()
	{
		conn=openDB();
	}
	// open database connection
	public Connection openDB()
	{
		try 
		{
			OracleDataSource ods = new OracleDataSource();

			if ( test.Main.dataConn == "GaryTallaght"){
				ods.setURL("jdbc:oracle:thin:@//10.10.2.7:1521/global1");
				ods.setUser("X00106491");
				ods.setPassword("db13May85");
			}
			else if ( test.Main.dataConn == "KeythTallaght"){
				ods.setURL("jdbc:oracle:thin:@//10.10.2.7:1521/global1");
				ods.setUser("X00106271");
				ods.setPassword("db19Jan82");
			}
			else if ( test.Main.dataConn == "KeythHome"){
				ods.setURL("jdbc:oracle:thin:HR/localhost:1521:XE");
				ods.setUser("System");
				ods.setPassword("7481600kW");
			}
			else if ( test.Main.dataConn == "GaryHome"){
				ods.setURL("jdbc:oracle:thin:HR/SYSTEM@localhost:1521:XE");
				ods.setUser("SYSTEM");
				ods.setPassword("1nf1n1ty");
			}

			conn = ods.getConnection();
			System.out.println("connected main.");
		} 
		catch (Exception e) 
		{
			System.out.print("Unable to load driver " + e);
			System.exit(1);
		}
		return conn;
	}
	
	// return true or false if a users table exists
	public boolean checkTable() 
	{
		boolean a=false;
		try 
		{
			String queryString = "select count(*) from all_objects where "
					+ "object_type in ('TABLE') and object_name = 'USERS'";

			stm = conn.createStatement();
			ResultSet rst = stm.executeQuery(queryString);
			/*while(rst.next())
			{
				if(rst.getInt(1)==1)
				{
					a=true;
				}
			}*/
		} 
		catch (Exception e) 
		{
			System.out.println(e);
		}
		return a;
	}


	
	// close the connection to database
	public void closeDB() 
	{
		try 
		{
			stm.close();
			conn.close();
			System.out.print("Connection closed main");
		} 
		catch (SQLException e) 
		{
			System.out.print("Could not close connection ");
			e.printStackTrace();
		}
	}
}
